/**
 * 
 */
package Controller;

import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;

import javax.swing.JFileChooser;
import javax.swing.JLabel;

import Model.ContactList;
import Model.contact;
import View.MainFrame;

/**
 * @author enzo
 *
 */
public class AgendaManager {

	private MainFrame mf;
	private ContactList addressBook;
	//Thread model;
	/**
	 * 
	 */
	public AgendaManager()
	{
		addressBook=new ContactList();
		//model=new Thread(addressBook);
		//model.start();
		addressBook.loadAgenda();
		mf=new MainFrame(addressBook.getList());
		mf.addWindowListener(new WinListener(this));
		addressBook.addObserver(mf.getContactList());
		mf.getFormView().getCreateContact().addActionListener(new CreateListener(this));
		mf.getContactList().getRemove().addActionListener(new RemoveListener(this));
		mf.getContactList().getContactList().addListSelectionListener(new SelectionListener(this));
		mf.getContactList().getAdd().addActionListener(new AddListener(this));
		mf.getDisplay().getModify().addActionListener(new AlterListener(this));
		mf.getFormView().getSaveAlter().addActionListener(new SaveListener(this));
		mf.getDisplay().getUrl().addMouseListener(new LinkListener(this));
		mf.getDisplay().getEmailPro().addMouseListener(new LinkListener(this));
		mf.getDisplay().getEmailPerso().addMouseListener(new LinkListener(this));
		mf.getSortMenu().getAlpha().addActionListener(new MenuListener(this));
		mf.getSortMenu().getGroup().addActionListener(new MenuListener(this));
		mf.getFormView().getBrowse().addActionListener(new BrowseListener(this));
		mf.getFormView().getFileChooser().addPropertyChangeListener(new PreviewListener(this));
	}
	/**
	 * 
	 */
	public void addContact()
	{ 
		ArrayList <String> info = mf.getFormView().retrieveFormInfo();
		contact c= new contact(info.get(0),info.get(1), info.get(2), info.get(3), info.get(4), info.get(5), info.get(6), info.get(7), info.get(8), info.get(9), info.get(10));
		/*c.setName(info.get(0));
		c.setFirstname(info.get(1));
		c.setAddress(info.get(2));
		c.setCompanyAddress(info.get(3));
		c.setURL(info.get(6));
		c.setEmailPerso(info.get(7));
		c.setEmailPro(info.get(8));
		c.setPhoto("Resources/Mysterion.png");*/
		addressBook.addContact(c);
		mf.getFormView().cleanForm();
		
	}
	/**
	 * 
	 */
	public void removeContact()
	{
		addressBook.removeContact((contact)mf.getContactList().getContactList().getSelectedValue());
		
	}
	/**
	 * 
	 */
	public void alterContact()
	{
		contact c =(contact)mf.getContactList().getContactList().getSelectedValue();
		mf.switchToAlterView(c);
	}
	/**
	 * 
	 */
	public void saveAlteration()
	{
		contact c =(contact)mf.getContactList().getContactList().getSelectedValue();
		ArrayList<String> info =mf.getFormView().retrieveFormInfo();
		c.setName(info.get(0));
		c.setFirstname(info.get(1));
		c.setAddress(info.get(2));
		c.setCompanyAddress(info.get(3));
		c.setPhoneperso(info.get(4));
		c.setPhonepro(info.get(5));
		c.setEmailPerso(info.get(6));
		c.setEmailPro(info.get(7));
		c.setURL(info.get(8));
		c.setPhoto(info.get(9));
		c.setGroup(info.get(10));
		mf.getFormView().cleanForm();
		mf.getDisplay().displayContact(c);
		mf.switchToContactView();
	}
	public void displayContact()
	{	
		contact c=(contact)mf.getContactList().getContactList().getSelectedValue();
		mf.getDisplay().displayContact(c);
		mf.switchToContactView();
	}
	public void openForm()
	{
		mf.getFormView().cleanForm();
		mf.switchToFormView();
	}
	//deprecated
	/**
	 * deprecated
	 */
	public void actualizeList()
	{
		mf.getContactList().cloneContact(addressBook.getList());
		mf.getContactList().update();
	}
	public void saveAddressBook()
	{
		addressBook.saveAgenda();
	}

	/**
	 * @throws IOException
	 * @throws URISyntaxException
	 */
	public void openWebsite() throws IOException, URISyntaxException
	{
		JLabel label=mf.getDisplay().getUrl();
        String plainText = label.getText().replaceAll("\\<.*?\\>", "");
        plainText.trim();
        Desktop.getDesktop().browse(new URI(plainText));
	}
	/**
	 * @param to
	 * @throws IOException
	 * @throws URISyntaxException
	 */
	public void sendMailto(JLabel to) throws IOException, URISyntaxException
	{
		JLabel label=to;
        String plainText = label.getText().replaceAll("\\<.*?\\>", "");
        plainText.trim();
        Desktop.getDesktop().mail(new URI("mailto:"+plainText));
	}
	public void browse()
	{
		if (mf.getFormView().getFileChooser().showOpenDialog(null)==JFileChooser.APPROVE_OPTION)
		{
			mf.getFormView().browse();
		}
	}
	public void alphaSort()
	{
		addressBook.alphaSort();
		//addressBook.setaSort(true);
		
	}
	public void groupSort()
	{
		addressBook.groupSort();
		//addressBook.setgSort(true);
	}
	/**
	 * @return the mf
	 */
	public MainFrame getMf() {
		return mf;
	}
	public void preview()
	{
		mf.getFormView().preview();
		
	}
	/**
	 * @param args
	 */
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		AgendaManager agenda = new AgendaManager();
		
		
		
	}
	/**
	 * @return the addressBook
	 */
	public ContactList getAddressBook() {
		return addressBook;
	}


}
